﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Hadron
{
    enum GameFontAlignment
    {
        Left,
        Centre,
        Right
    }

    class GameFont
    {
        private SpriteFont m_spriteFont;
        private Color m_colourOutline, m_colourFill;
        private GameFontAlignment m_gameFontAlignment;

        public GameFont(SpriteFont p_spriteFont)
        {
            m_spriteFont = p_spriteFont;
            m_colourFill = Color.White;
            m_colourOutline = Color.Black;
        }

        public Vector2 MeasureString(String p_strText)
        {
            Vector2 vecSize = m_spriteFont.MeasureString(p_strText);
            vecSize.X += 2.0f;
            vecSize.Y += 2.0f;
            return vecSize;
        }

        public void DrawString(SpriteBatch p_spriteBatch, Vector2 p_vecPosition, String p_strText)
        {
            if (m_gameFontAlignment != GameFontAlignment.Left)
            {
                Vector2 vecSize = MeasureString(p_strText);
                if (m_gameFontAlignment == GameFontAlignment.Centre)
                    p_vecPosition.X -= vecSize.X * 0.5f;
                else
                    p_vecPosition.X -= vecSize.X;
            }

            p_spriteBatch.DrawString(m_spriteFont, p_strText, p_vecPosition - Vector2.UnitX, m_colourOutline);
            p_spriteBatch.DrawString(m_spriteFont, p_strText, p_vecPosition + Vector2.UnitX, m_colourOutline);
            p_spriteBatch.DrawString(m_spriteFont, p_strText, p_vecPosition - Vector2.UnitY, m_colourOutline);
            p_spriteBatch.DrawString(m_spriteFont, p_strText, p_vecPosition + Vector2.UnitY, m_colourOutline);
            p_spriteBatch.DrawString(m_spriteFont, p_strText, p_vecPosition, m_colourFill);
        }

        public Vector2 DrawString(SpriteBatch p_spriteBatch, Vector2 p_vecPosition,
            float p_fWrapWidth, String p_strText)
        {
            p_strText = p_strText + " ";
            do
            {
                int nCount = 0, nWrapPoint = p_strText.Length - 1;
                while (nCount < p_strText.Length && MeasureString(p_strText.Substring(0, nCount)).X < p_fWrapWidth)
                {
                    if (p_strText[nCount] == ' ')
                        nWrapPoint = nCount;
                    ++nCount;
                }
                DrawString(p_spriteBatch, p_vecPosition, p_strText.Substring(0, nWrapPoint));
                p_vecPosition.Y += (m_spriteFont.LineSpacing + 2);
                p_strText = p_strText.Substring(nWrapPoint);
                while (p_strText.Length > 0 && p_strText[0] == ' ')
                    p_strText = p_strText.Substring(1);
            }
            while (p_strText.Length > 0);
            return p_vecPosition;
        }

        public Color Colour
        {
            get { return m_colourFill; }
            set
            {
                m_colourFill = value;
                m_colourOutline.A = m_colourFill.A;
            }
        }

        public GameFontAlignment Alignment
        {
            get { return m_gameFontAlignment; }
            set { m_gameFontAlignment = value; }
        }

        public float Opacity
        {
            get { return m_colourFill.ToVector3().Z; }
            set
            {
                m_colourFill.A = (byte)(value * 255.0f);
                m_colourOutline.A = m_colourFill.A;
            }
        }
    }
}
